package com.igelato.response;

import com.igelato.utils.ThreadLocalTool;

/**
 * @Description:
 * @Author: wht
 * @Date: 2023-12-30
 */
public class R<T> {
    //服务端返回的错误码
    private int code;
    //服务端返回的错误信息
    private String msg;
    //我们服务端返回的数据
    private T data;

    private String token;


    private R(int code, String msg, T data) {
        this.code = code;
        if (msg != null) {
            this.msg = msg;
        }
        if (data != null) {
            this.data = data;
        }
        this.token = ThreadLocalTool.getToken();
    }

    public static <T> R successMsg(String msg) {
        R res = new R(200, msg, null);
        return res;
    }

    public static <T> R successData(T data) {
        R res = new R(200, null, data);
        return res;
    }

    public static <T> R successMsgData(String msg, T data) {
        R res = new R(200, msg, data);
        return res;
    }

    public static <T> R error(ErrorCodeMsgEnum errorCodeMsgEnum) {
        R res = new R(errorCodeMsgEnum.getCode(), errorCodeMsgEnum.getMsg(), null);
        return res;
    }

    public static <T> R error(Integer code, String msg) {
        R res = new R(code, msg, null);
        return res;
    }

    public int getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public String getToken() {
        return this.token;
    }

    public T getData() {
        return this.data;
    }
}
